﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using HA.Data;
using HAVTKPack;
using HA.ModFlow.Infrastructure;
using HA.ModFlow.Domain.DomainLayer;


namespace HA.ModFlow.Application
{
    /// <summary>
    /// 网格Node的管理：添加Node，删除Node，更新Node等等
    /// </summary>
    public static class GridNodeManager
    {

        #region 网格Grid的Node更新：添加，删除，高程，IBound，Z轴缩放，等等


        public static void AddGridNode(Scene vtkScene, double scaleZ)
        {
            if (vtkScene == null)
            {
                return;
            }
            if (scaleZ <0)
            {
                scaleZ = 1;   
            }

            //添加网格Node：包括两个Filter（Show和Contour）
            Project project = ProjectService.DefaultService.Project;

            if (project != null)
            {
                if (project.Grid3dData != null)
                {
                    if (project.Grid3dData.Grids != null)
                    {
                        if (project.Grid3dData.Grids.Count > 0)//主程序还没有建立任何网格
                        {

                            #region 添加Grid  Node

                            Grid3d gd = project.Grid3dData.MainGrid;//得到主程序建立的网格信息


                            List<int[,]> ibound = gd.ModFlowGrid.WaterGrid.GetIBounds();
                            HydrolicGrid hg = AlgorithmHelper.ConvertToHydroGrid(gd.ModFlowGrid.GetBoundGrid());
                          
                            
                            GeometryNode gnGrid = new GeometryNode(GridNodeIDManager.grid.ToString(), new HydrolicGridGeometry(hg, ibound));


                            Vector newVector = new Vector(1, 1, scaleZ);

                            gnGrid.Scale = newVector;
                            ((HydrolicGridGeometry)gnGrid.Geometry).UpdateTransformScale(newVector);


                            gnGrid.AddFilter(new ShowGeometryPolyDataFilter(FilterIDManager.showGridPolyDataFilter.ToString()));




                            #region ContourFilter

                            ContourSetup contourSetup = new ContourSetup();

                            // 更新ContourFilter中的配置信息

                            //UpdateContourSetup(ProjectSerivce.DefaultService.Project, vtkScene, contourSetup);
                            //UpdateFlowVectorSetup(ProjectSerivce.DefaultService.Project, vtkScene);


                            UpdateContourSetup2(ProjectService.DefaultService.Project, vtkScene, contourSetup);


                            ContourFilter contour = null;

                            #region

                            CellCenterDataSet3d grid3DCellCenterValue = ProjectService.DefaultService.Project.CurrentDSData;
                            if (grid3DCellCenterValue != null)
                            {
                                //更新Ibound

                                List<int[,]> cellDataIBound = new List<int[,]>();

                                for (int i = 0; i < grid3DCellCenterValue.EffIBound.Count; i++)
                                {
                                    int[,] layerIBound = (int[,])grid3DCellCenterValue.EffIBound[i].Clone();

                                    cellDataIBound.Add(layerIBound);
                                }


                                //contour = new ContourFilter(FilterIDManager.contour.ToString(), hg, cellDataIBound);
                                contour = new ContourFilter(FilterIDManager.contour.ToString(), hg, cellDataIBound, contourSetup);
                                

                                //更新CellScalar值

                                List<double[,]> gridCellScalar = new List<double[,]>();

                                double[,] layerCellScalar = new double[grid3DCellCenterValue.Nrows, grid3DCellCenterValue.Ncols];

                                bool getData = false;

                                getData = grid3DCellCenterValue.GetAllGridData(gridCellScalar);

                                if (getData)
                                {
                                    contour.ContourData = gridCellScalar;

                                }
                            }
                            else
                            {
                                //contour = new ContourFilter(hg);

                                //contour = new ContourFilter(FilterIDManager.contour.ToString(), hg, ibound);
                                contour = new ContourFilter(FilterIDManager.contour.ToString(), hg, ibound, contourSetup);

                            }


                            contour.UpdateTransformScale(newVector);

                            #endregion

                            gnGrid.AddFilter(contour);

                            #endregion



                            #region FllowVectorFilter
                            HydrolicGrid hgHead = AlgorithmHelper.ConvertToHydroGrid(gd.ModFlowGrid.GetHeadGrid());
                            VectorFilter vectorFilter = null;

                            #region


                            VectorSetup setup = new VectorSetup();

                            //取得主程序的设置   显示设置

                            //UpdateVectorSetup(ProjectSerivce.DefaultService.Project, vtkScene, setup);


                            UpdateVectorSetup2(ProjectService.DefaultService.Project, vtkScene, setup);


                            Vector[, ,] vectorData = null;

                            //取得主程序的设置    cell数据
                          

                            CellCenterVector3dDS flowVectorDS = ProjectService.DefaultService.Project.CurentFlowVectorData ;

                            if (flowVectorDS != null)
                            {
                                #region IBound,VectorData

                                //更新Ibound


                                List<int[,]> cellDataIBound = new List<int[,]>();

                                for (int i = 0; i < flowVectorDS.EffIBound.Count; i++)
                                {
                                    int[,] layerIBound = (int[,])flowVectorDS.EffIBound[i].Clone();

                                    cellDataIBound.Add(layerIBound);
                                }


                                int layerN = flowVectorDS.EffIBound.Count;
                                int rowN = 0;
                                int colN = 0;
                                if (layerN > 0)
                                {
                                    rowN = flowVectorDS.EffIBound[0].GetLength(0);
                                    colN = flowVectorDS.EffIBound[0].GetLength(1);
                                }

                                vectorData = new Vector[rowN, colN, layerN];

                                for (int layer = 0; layer < layerN; layer++)
                                {
                                    for (int row = 0; row < rowN; row++)
                                    {
                                        for (int col = 0; col < colN; col++)
                                        {

                                            Vector vector = new Vector(0,0,0);


                                            vector.X = flowVectorDS.Vectors[row, col, layer].vx;
                                            vector.Y = flowVectorDS.Vectors[row, col, layer].vy;
                                            vector.Z = flowVectorDS.Vectors[row, col, layer].vz;

                                            vectorData[row, col, layer] = vector;

                                        }

                                    }

                                }

                                #endregion

                                vectorFilter = new VectorFilter(FilterIDManager.vectorFilter.ToString(), hgHead, cellDataIBound, vectorData, setup);

                            }
                            else
                            {

                                vectorFilter = new VectorFilter(FilterIDManager.vectorFilter.ToString(), hgHead, ibound, vectorData, setup);

                            }



                            //缩放
                            //vectorFilter.UpdateTransformScale(newVector);
                            vectorFilter.ScaleVector = newVector;

                            #endregion

                            gnGrid.AddFilter(vectorFilter);

                            #endregion




                            vtkScene.AddNode(gnGrid);


                           


                            #endregion


                        }
                    }
                }
            }
        }


        public static void DeleteGridNode(Scene vtkScene)
        {
            if (vtkScene != null)
            {

                vtkScene.RemoveNode(GridNodeIDManager.grid.ToString());



            }


        }


        public static void UpdateGeometryElevation(Scene vtkScene, Grid3d grid)
        {
            if (vtkScene == null || grid == null)
            {
                return;
            }

            //GridNodeIDManager gridNodeID;
            //switch (gridNodeID)
            //{
            //    case GridNodeIDManager.grid:

            //        break;

            //    default:
            //        break;
            //}

            HydrolicGrid hg = AlgorithmHelper.ConvertToHydroGrid(grid.ModFlowGrid .GetBoundGrid ());
            ((HydrolicGridGeometry)(((GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString())).Geometry)).Hg = hg;


            GeometryNode gnGrid = vtkScene.GetNode(GridNodeIDManager.grid.ToString()) as GeometryNode;

            ContourFilter contour = gnGrid.GetFilter(FilterIDManager.contour.ToString()) as ContourFilter;
            contour.Hg = hg;


            HydrolicGrid hgHead = AlgorithmHelper.ConvertToHydroGrid(grid .ModFlowGrid.GetHeadGrid ());
            VectorFilter vectorFilter = gnGrid.GetFilter(FilterIDManager .vectorFilter .ToString ()) as VectorFilter ;
            vectorFilter.Hg = hgHead;


        }


        public static void UpdateGeometryIBound(Scene vtkScene, List<int[,]> effectSign)
        {
            if (vtkScene == null || effectSign == null)
            {
                return;
            }


            GeometryNode gnGrid = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());

            ((HydrolicGridGeometry)(gnGrid.Geometry)).EffectiveSign1 = effectSign;


            ////主程序规定：ContourFilter 不响应IBoundChange事件
            //ContourFilter cf = (ContourFilter)gnGrid.GetFilter(FilterIDManager.contour.ToString());
            //cf.EffectiveSign1 = effectSign;


            VectorFilter vf = (VectorFilter)gnGrid.GetFilter(FilterIDManager.vectorFilter.ToString());
            vf.Ibound = effectSign;


        }


        public static void UpdateGeometryTransformZScale(Scene vtkScene,double scaleZ)
        {
            if (vtkScene ==null ||scaleZ <0)
            {
                return;
                
            }
            if (vtkScene .NodeDic ==null )
            {
                return;
            }

            //遍历所有节点 更新transform缩放

            foreach (var item in vtkScene.NodeDic)
            {

                if (item.Value.Name == GridNodeIDManager.grid.ToString())
                {


                    #region  更新节点Node的transform缩放

                    HAVTKPack.Vector scale = new Vector(1, 1, scaleZ);

                    GeometryNode geometryNode = (GeometryNode)item.Value;

                    geometryNode.Geometry.GeometryTransform.Identity();

                    geometryNode.Scale = scale;


                    #endregion


                    #region 有特殊的filter：ContourFilter(它没有使用它的Node的数据，而是包含了原始数据，并且对原始数据进行了大量的自己的操作)和 VectorFilter。

                    // 这个结构让我很纠结 。。。痛苦呀。。。。。。。

                    if (item.Value.Name == GridNodeIDManager.grid.ToString())
                    {

                        foreach (var filter in item.Value.Filters)
                        {
                            #region
                            if (filter.Id == FilterIDManager.contour.ToString())
                            {
                                ContourFilter contourFilter = (ContourFilter)filter;


                                contourFilter.ScaleVector = scale;
         

                             
                            }
                            #endregion

                            #region

                            if (filter.Id == FilterIDManager.vectorFilter .ToString())
                            {
                                VectorFilter vectorFilter = (VectorFilter)filter;

                                vectorFilter.ScaleVector = scale;


                                break;
                            
                            }

                            #endregion


                        }

                    }


                    #endregion


                }

            }

        }



        public static void UpdateShowNodeFilter()
        {


        }


        #endregion



        #region 具体的Grid的Node的ContourFilter的配置更新：ScalarData，scalarAndIBound,等值线和颜色，更新流水线对象参数   等等


        /// <summary>
        /// 首先取得主程序ContourSetup  然后更新到 Filter 中
        /// </summary>
        /// <param name="project"></param>
        /// <param name="vtkScene"></param>
        public static void UpdateContourSetupToFilter(Project project, Scene vtkScene)
        {
            if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }

            try
            {

                ContourSetup contourSetup = new ContourSetup();
                UpdateContourSetup(project, vtkScene, contourSetup);
               
                GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());
                ContourFilter cf = (ContourFilter)gn.GetFilter(FilterIDManager.contour.ToString());
              
                cf.ContourSetup = contourSetup;

            }
            catch
            {


            }

        }

        /// <summary>
        /// 首先取得主程序ContourSetup  然后更新到 Filter 中
        /// </summary>
        /// <param name="project"></param>
        /// <param name="vtkScene"></param>
        public static void UpdateContourSetupToFilter2(Project project, Scene vtkScene)
        {
            if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }

            try
            {

                ContourSetup contourSetup = new ContourSetup();
                UpdateContourSetup2 (project, vtkScene, contourSetup);//从主程序取得新的显示选项配置信息

                GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());
                ContourFilter cf = (ContourFilter)gn.GetFilter(FilterIDManager.contour.ToString());

                cf.ContourSetup = contourSetup;//触发属性改变事件

            }
            catch
            {


            }

        }



        /// <summary>
        ///  根据ContourFilter中的ContourSetup数据 更新 vtkLookupTable和vtkContourFilter的参数设置
        /// 一般调用该方法前先要调用UpdateContourSetup方法更新配置信息
        /// </summary>
        /// <param name="vtkScene"></param>
        //public static void UpdateLookupTableAndContoourFilter(Scene vtkScene)
        //{

        //    GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());

        //    if (gn != null)
        //    {
        //        ContourFilter cf = (ContourFilter)gn.GetFilter(FilterIDManager.contour.ToString());

        //        if (cf != null)
        //        {

        //            try
        //            {
        //                cf.UpdateLookupTableAndContoourFilter();
        //            }
        //            catch
        //            {
        //                return;
        //            }

        //        }

        //    }

        //}


        public static void UpdateContourIBoundAndSclarData(Scene vtkScene, List<double[,]> cellScalar, List<int[,]> cellIBound)
        {
            if (vtkScene == null || cellScalar == null || cellIBound == null)
            {
                return;
            }


            GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());
            ContourFilter cf = (ContourFilter)gn.GetFilter(FilterIDManager.contour.ToString());


            //cf.ContourData = gridCellScalar;
            //cf.EffectiveSign1 = grid3dCellCenterValue.EffIBound;

            ////cf.ContourData = gridCellScalar;

            cf.UpdateIBoundAndSclarData(cellScalar, cellIBound);







        }




        public static void UpdateFlowVectorSetupToFilter(Project project, Scene vtkScene)
        { 
          if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }

            try
            {
               



                VectorSetup setup = new VectorSetup();


                UpdateVectorSetup(project, vtkScene, setup);



                GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());


                VectorFilter vf = gn.GetFilter(FilterIDManager.vectorFilter.ToString()) as VectorFilter;

                vf.VectorSetup.SetVectorSetup(setup);

                vf.UpdateArrowSourceGlyph3dFilter();

            }
            catch
            {

              
            }




        }

        public static void UpdateFlowVectorSetupToFilter2(Project project, Scene vtkScene)
        {
            if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }

            try
            {




                VectorSetup setup = new VectorSetup();


                UpdateVectorSetup2(project, vtkScene, setup);



                GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());


                VectorFilter vf = gn.GetFilter(FilterIDManager.vectorFilter.ToString()) as VectorFilter;

                vf.VectorSetup.SetVectorSetup(setup);

                vf.UpdateArrowSourceGlyph3dFilter();

            }
            catch
            {


            }




        }




        //public static void UpdateArrowSourceGlyph3dFilter(Scene vtkScene)
        //{

        //    GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());

        //    if (gn != null)
        //    {
               
        //        VectorFilter vf = gn.GetFilter(FilterIDManager .vectorFilter .ToString ()) as VectorFilter;

        //        if (vf != null)
        //        {

        //            try
        //            {
        //                vf.UpdateArrowSourceGlyph3dFilter ();
        //            }
        //            catch
        //            {
        //                return;
        //            }

        //        }

        //    }

        
        //}

        public static void UpdateIBoundAndFlowVectorData(Scene vtkScene,Vector [,,] vectorData, List<int[,]> cellIBound)
        {
            if (vtkScene == null || vectorData == null || cellIBound == null)
            {
                return;
            }

            GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());


            VectorFilter vf = (VectorFilter)gn.GetFilter(FilterIDManager.vectorFilter .ToString());

            vf.UpdateIBoundAndVectorData(vectorData,cellIBound );


        
        }


        public static void UpdateContourScalarData(Scene vtkScene, List<double[,]> cellScalar)
        {
            if (vtkScene == null || cellScalar == null)
            {
                return;
            }


            GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());
            ContourFilter cf = (ContourFilter)gn.GetFilter(FilterIDManager.contour.ToString());

            cf.ContourData = cellScalar;


        }

        public static void UpdateFlowVectorData(Scene vtkScene, Vector[, ,] vectorData) 
        {

            if (vtkScene == null )
            {
                return;
            }


            GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());
            VectorFilter vf = (VectorFilter)gn.GetFilter(FilterIDManager.vectorFilter.ToString());

            vf.CellVectorData1 = vectorData;
        
        }





        public static void UpdateContourLineVisible(Project project, Scene vtkScene)
        {


            if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }
            if (project != null && project.DisplayOption2 != null && project.DisplayOption2.DoGrid3dData != null && project.DisplayOption2.DoGrid3dData.Grid3d != null)
            {

            }
            else
            {
                return;
            }
            try
            {
                GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());

                ContourFilter cf = gn.GetFilter(FilterIDManager.contour.ToString()) as ContourFilter;

                //if (cf != null)
                //{
                //    cf.Visiability = project.DisplayOption2.DoGrid3dData.Grid3d.IsShowContour;
                //}
                cf.UpdateContourLineVisibility(project.DisplayOption2.DoGrid3dData.Grid3d.IsShowContour);


            }
            catch
            {


            }

        }

        public static  void UpdateContourLineCONTOURMODE(Project project, Scene vtkScene)
        {
            if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }
            if (project != null && project.DisplayOption2 != null && project.DisplayOption2.DoGrid3dData != null && project.DisplayOption2.DoGrid3dData.Grid3d != null)
            {

            }
            else
            {
                return;
            }
            try
            {
                GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());

                ContourFilter cf = gn.GetFilter(FilterIDManager.contour.ToString()) as ContourFilter;

                //cf.UpdateContourLineCONTOURMODE(project.DisplayOption2.DoGrid3dData.Grid3d.Contour .ContourType );
               

            }
            catch
            {


            }
        }

        public static void UpdateContourLineValueList(Project project, Scene vtkScene)
        {
            if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }
            if (project != null && project.DisplayOption2 != null && project.DisplayOption2.DoGrid3dData != null && project.DisplayOption2.DoGrid3dData.Grid3d != null && project.DisplayOption2.DoGrid3dData.Grid3d.Contour !=null )
            {

            }
            else
            {
                return;
            }

            try
            {
               


                HA.Data.DisplayOptions2.DOGrid3d doGrid3d = project.DisplayOption2.DoGrid3dData.Grid3d;

                if (doGrid3d == null)
                {
                    return;
                }
                //DOContourGeneral contourGeneral = doGrid3d.ContourOption.ContourGeneral;//从主程序取得显示选项控制信息
                HA.Data .DisplayOptions2 .DOContour  contourOptionV2 = doGrid3d.Contour;

                if (contourOptionV2 == null)
                {
                    return;
                }

                if (contourOptionV2.ContourLines == null)
                {
                    return;
                }
             
                Dictionary<double, System.Drawing.Color> contourLines = new Dictionary<double, System.Drawing.Color>();


                for (int i = 0; i < contourOptionV2.ContourLines.Count; i++)
                {
                    System.Drawing.Color color = System.Drawing.Color.FromArgb(contourOptionV2.ContourLines[i].Color.A, contourOptionV2.ContourLines[i].Color.R, contourOptionV2.ContourLines[i].Color.G, contourOptionV2.ContourLines[i].Color.B);

                    contourLines.Add(contourOptionV2.ContourLines[i].Value, color);

                }

                GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());

                ContourFilter cf = gn.GetFilter(FilterIDManager.contour.ToString()) as ContourFilter;

                cf.UpdateContourLineValueList(contourLines);


            }
            catch 
            {
              
            }


        }

        public static void UpdateContourLineProperty(Project project, Scene vtkScene)
        {

              if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }
            if (project != null && project.DisplayOption2 != null && project.DisplayOption2.DoGrid3dData != null && project.DisplayOption2.DoGrid3dData.Grid3d != null && project.DisplayOption2.DoGrid3dData.Grid3d.Contour !=null )
            {

            }
            else
            {
                return;
            }

            try

            {

                //由于二维需要
                // 因为在等值线List集合中，如果value不变，颜色改变走属性变化事件，所以为避免错过颜色的更新，首先需要更新List集合
                UpdateContourLineValueList(project, vtkScene);



               HA.Data.DisplayOptions2.  DOGrid3d doGrid3d = project.DisplayOption2 .DoGrid3dData .Grid3d;

                if (doGrid3d == null)
                {
                    return;
                }
                //DOContourGeneral contourGeneral = doGrid3d.ContourOption.ContourGeneral;//从主程序取得显示选项控制信息
                HA.Data .DisplayOptions2 .DOContour  contourOptionV2 = doGrid3d.Contour;

                if (contourOptionV2 == null)
                {
                    return;
                }

               


                ContourLineProperty contourLineProperty = new ContourLineProperty();

                switch (contourOptionV2.ContourType )
                {
                    case ContourDisplayType.Linear:
                        contourLineProperty.ContourFillOrLine = CONTOURTYPE.Linear;
                        break;
                    case ContourDisplayType.ColorFill:
                        contourLineProperty.ContourFillOrLine = CONTOURTYPE.ColorFill ;
                        break;
                    case ContourDisplayType.LinearAndColorFill:
                        contourLineProperty.ContourFillOrLine = CONTOURTYPE.LinearAndColorFill ;
                        break;
                    default:
                        break;
                }

                contourLineProperty.IsSpecifyPrecision = contourOptionV2.IsSpecifyPrecision;

                //if (contourOptionV2.IsSpecifyPrecision)
                //{
                //    contourLineProperty.DecimalDigitNum = contourOptionV2.DecimalDigitNum;
                //}
                //else
                //{
                //    contourLineProperty.DecimalDigitNum = contourOptionV2.NoSpecifyPrecisionDecimalNum ;
                //}
                contourLineProperty.DecimalDigitNum = contourOptionV2.DecimalDigitNum;

                contourLineProperty.IsUseScientificNotation = contourOptionV2.IsUseScientificNotation;



                contourLineProperty.IsUseCellRealData = contourOptionV2.IsUseCellRealData;


                contourLineProperty.LineWidth = contourOptionV2.LineWidth;

                contourLineProperty.LineColor  = contourOptionV2.LineColor ;


                GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());

                ContourFilter cf = gn.GetFilter(FilterIDManager.contour.ToString()) as ContourFilter;


                cf.UpdateContourLineProperty(contourLineProperty);



            }
            catch 
            {
               
               
            }
 

        }
        public static void UpdateScalarBar(Project project, Scene vtkScene)
        {

            if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }
            if (project != null && project.DisplayOption2 != null && project.DisplayOption2.DoGrid3dData != null && project.DisplayOption2.DoGrid3dData.Grid3d != null && project.DisplayOption2.DoGrid3dData.Grid3d.Contour != null && project .DisplayOption2 .DoGrid3dData .Grid3d .Contour .ContourLineProperty .LineScalarBar !=null )
            {

            }
            else
            {
                return;
            }

            try
            {


                HA.Data.DisplayOptions2.DOGrid3d doGrid3d = project.DisplayOption2.DoGrid3dData.Grid3d;

                if (doGrid3d == null)
                {
                    return;
                }
                //DOContourGeneral contourGeneral = doGrid3d.ContourOption.ContourGeneral;//从主程序取得显示选项控制信息
                HA.Data.DisplayOptions2.DOContour contourOptionV2 = doGrid3d.Contour;

                if (contourOptionV2 == null)
                {
                    return;
                }

                HA.Data.DisplayOptions2.ContourLineScalarBar contourLinescalarBar = doGrid3d.Contour.ContourLineProperty.LineScalarBar;



                ContourScalarBar scalarBar = new ContourScalarBar();


                scalarBar.IsShowScalarBar = contourLinescalarBar.IsShowScalarBar;
                scalarBar.IsDragable = contourLinescalarBar.IsDragable;
                scalarBar.Title = contourLinescalarBar.Title;
                scalarBar.TitleColor = contourLinescalarBar.TitleColor;
                scalarBar.TitleFontSize = contourLinescalarBar.TitleFontSize;
                scalarBar.LabelColor = contourLinescalarBar.LabelColor;
                scalarBar.LabelFontSize = contourLinescalarBar.LabelFontSize;




                //主程序认为视口的左上角点为  0  0  点
                //vtk 图例的位置设置的为图例左下角交点坐标  需要转换

                int sizeW = vtkScene.VtkRenderWindow.GetSize()[0];//视口宽度   像素
                int sizeH = vtkScene.VtkRenderWindow.GetSize()[1];//视口高度  像素

                //scalarBar.YLowLeftToViewPort = sizeH - contourLinescalarBar.Height * sizeH - contourLinescalarBar.YToViewPortLeftUper;
                //scalarBar.Width = contourLinescalarBar.Width;
                //scalarBar.Height = contourLinescalarBar.Height;
                //scalarBar.XLowLeftToViewPort = contourLinescalarBar.XToViewPortLeftUper;
       

                //scalarBar.Width = 0.04;
                //scalarBar.Height = 0.2;
                //scalarBar.XLowLeftToViewPort = sizeW * 0.5;
                //scalarBar.YLowLeftToViewPort = sizeH * 0.5;
                //width  height  coor  转换到0-1范围
                scalarBar.Width =(double ) contourLinescalarBar.Width * 0.01;
                scalarBar.Height = (double )contourLinescalarBar.Height * 0.01;
                //scalarBar.Height = 0.5;
                scalarBar.XLowLeftToViewPort = (double)contourLinescalarBar.XToViewPortLeftUper * 0.01;
                scalarBar.YLowLeftToViewPort = (double)(sizeH - contourLinescalarBar.Height * 0.01 * sizeH - contourLinescalarBar.YToViewPortLeftUper * 0.01 * sizeH) / (double)sizeH;

                //scalarBar.YLowLeftToViewPort = (double)(sizeH - 50 * 0.01 * sizeH - contourLinescalarBar.YToViewPortLeftUper * 0.01 * sizeH) / (double)sizeH;


                GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());

                ContourFilter cf = gn.GetFilter(FilterIDManager.contour.ToString()) as ContourFilter;


                cf.UpdateScalarBar(scalarBar);



            }
            catch
            {


            }

        }
        public static void UpdateTagging(Project project, Scene vtkScene)
        {
            if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }
            if (project != null && project.DisplayOption2 != null && project.DisplayOption2.DoGrid3dData != null && project.DisplayOption2.DoGrid3dData.Grid3d != null && project.DisplayOption2.DoGrid3dData.Grid3d.Contour != null && project.DisplayOption2.DoGrid3dData.Grid3d.Contour.ContourLineProperty.LineScalarBar != null)
            {

            }
            else
            {
                return;
            }

            try
            {


                HA.Data.DisplayOptions2.DOGrid3d doGrid3d = project.DisplayOption2.DoGrid3dData.Grid3d;

                if (doGrid3d == null)
                {
                    return;
                }
                //DOContourGeneral contourGeneral = doGrid3d.ContourOption.ContourGeneral;//从主程序取得显示选项控制信息
                HA.Data.DisplayOptions2.DOContour contourOptionV2 = doGrid3d.Contour;

                if (contourOptionV2 == null)
                {
                    return;
                }

                HA.Data.DisplayOptions2.ContourLineTagging  contourLineTagging = doGrid3d.Contour.ContourLineProperty.LineTagging ;



                ContourTagging tagging = new ContourTagging();


                tagging.IsShowTagging = contourLineTagging.IsShowTagging;
                tagging.TagColor = contourLineTagging.TagColor;
                tagging.FontSize = contourLineTagging.FontSize;
                tagging.MaskPointNum = contourLineTagging.MaskPointNum;





                GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());

                ContourFilter cf = gn.GetFilter(FilterIDManager.contour.ToString()) as ContourFilter;


                cf.UpdateTagging (tagging);



            }
            catch
            {


            }
        }



        public static void UpdateVectorVisivle(Project project, Scene vtkScene)
        {
            if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;


            }
            if (project != null && project.DisplayOption2 != null && project.DisplayOption2.DoGrid3dData != null && project.DisplayOption2.DoGrid3dData.Grid3d != null)
            {

            }
            else
            {
                return;
            }
            try
            {
                GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());


                VectorFilter vf = gn.GetFilter(FilterIDManager.vectorFilter.ToString()) as VectorFilter;

                if (vf != null)
                {
                    //vf.Visiability = project.DisplayOption2.DoGrid3dData.Grid3d.IsShowVector;

                    vf.UpdateVectorVisibility(project.DisplayOption2.DoGrid3dData.Grid3d.IsShowVector);

                }


            }
            catch
            {


            }

        }


        #endregion


        #region 内部辅助方法

        /// <summary>
        /// 根据主程序的等值线和颜色更新到ContourSetup数据中
        /// </summary>
        /// <param name="project"></param>
        /// <param name="vtkScene"></param>
        private static void UpdateContourSetup(Project project, Scene vtkScene, ContourSetup contourSetup)
        {


            if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }

            try
            {

                #region


                //if (contourSetup == null)
                //{
                //    contourSetup = new ContourSetup();
                //}


                //DOGrid3d doGrid3d = project.DisplayOption.Grid3d;

                //if (doGrid3d == null)
                //{
                //    return;
                //}
                ////DOContourGeneral contourGeneral = doGrid3d.ContourOption.ContourGeneral;//从主程序取得显示选项控制信息
                //DOContourOptionV2 contourOptionV2 = doGrid3d.Contour;

                //if (contourOptionV2 == null)
                //{
                //    return;
                //}

                //if (contourOptionV2.ContourLines == null)
                //{
                //    return;
                //}
                ////if (contourOptionV2.ContourLines.Count <= 0)
                ////{
                ////    return;
                ////}



                //contourSetup.UpdateVisibility(doGrid3d.IsShowContour);

                //contourSetup.UpdateContourModel(CONTOURMODE.LayerContours);



                //Dictionary<double, System.Drawing.Color> contourLines = new Dictionary<double, System.Drawing.Color>();


                //for (int i = 0; i < contourOptionV2.ContourLines.Count; i++)
                //{
                //    System.Drawing.Color color = System.Drawing.Color.FromArgb(contourOptionV2.ContourLines[i].Color.A, contourOptionV2.ContourLines[i].Color.R, contourOptionV2.ContourLines[i].Color.G, contourOptionV2.ContourLines[i].Color.B);

                //    contourLines.Add(contourOptionV2.ContourLines[i].Value, color);

                //}

                //contourSetup.ResetContours(contourLines);



                //#region
                ////GeometryNode gn = (GeometryNode)vtkScene.GetNode(GridNodeIDManager.grid.ToString());
                ////ContourFilter cf = (ContourFilter)gn.GetFilter(FilterIDManager.contour.ToString());


                ////cf.ContourMode = CONTOURMODE.LayerContours;


                ////switch (doGrid3d.ContourType)
                ////{
                ////    case ContourType.LayerContour:

                ////        cf.ContourMode = CONTOURMODE.LayerContours;
                ////        break;
                ////    case ContourType.GridContour:

                ////        cf.ContourMode = CONTOURMODE.GridContours;

                ////        break;
                ////    default:

                ////        break;
                ////}
                ////switch (contourGeneral.ContourType)
                ////{
                ////    case ContourDisplayType.Linear:

                ////        cf.ContourType = CONTOURTYPE.Linear;

                ////        break;
                ////    case ContourDisplayType.ColorFill:
                ////        cf.ContourType = CONTOURTYPE.ColorFill;

                ////        break;
                ////    case ContourDisplayType.LinearAndColorFill:

                ////        cf.ContourType = CONTOURTYPE.LinearAndColorFill;
                ////        break;

                ////    default:
                ////        break;
                ////}

                ////switch (contourOptionV2.ContourType)
                ////{
                ////    case ContourDisplayType.Linear:

                ////        cf.ContourType = CONTOURTYPE.Linear;

                ////        break;
                ////    case ContourDisplayType.ColorFill:
                ////        cf.ContourType = CONTOURTYPE.ColorFill;

                ////        break;
                ////    case ContourDisplayType.LinearAndColorFill:

                ////        cf.ContourType = CONTOURTYPE.LinearAndColorFill;
                ////        break;

                ////    default:
                ////        break;
                ////}

                ////Dictionary<double, System.Drawing.Color> contourLines = new Dictionary<double, System.Drawing.Color>();


                ////for (int i = 0; i < contourOptionV2.ContourLines.Count; i++)
                ////{
                ////    System.Drawing.Color color = System.Drawing.Color.FromArgb(contourOptionV2.ContourLines[i].Color.A, contourOptionV2.ContourLines[i].Color.R, contourOptionV2.ContourLines[i].Color.G, contourOptionV2.ContourLines[i].Color.B);

                ////    contourLines.Add(contourOptionV2.ContourLines[i].Value, color);

                ////}

                ////cf.ContourSetup.ResetContours(contourLines);
                ////cf.ContourSetup.ResetContours(contourLines, contourOptionV2.Min, contourOptionV2.Max);
                //#endregion



                #endregion



            }
            catch
            {

                System.Windows.Forms.MessageBox.Show("设置三维模块颜色映射的参数时出错！ ");
                return;
            }




        }



        /// <summary>
        /// 根据主程序的等值线和颜色更新到ContourSetup数据中
        /// </summary>
        /// <param name="project"></param>
        /// <param name="vtkScene"></param>
        private static void UpdateContourSetup2(Project project, Scene vtkScene, ContourSetup contourSetup)
        {


            if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }
            if (project != null && project.DisplayOption2 != null && project.DisplayOption2.DoGrid3dData != null && project.DisplayOption2.DoGrid3dData.Grid3d != null)
            {

            }
            else
            {
                return;
            }
            try
            {

                #region


                if (contourSetup == null)
                {
                    contourSetup = new ContourSetup();
                }


                HA.Data.DisplayOptions2 .  DOGrid3d doGrid3d = project.DisplayOption2.DoGrid3dData .Grid3d;

                if (doGrid3d == null)
                {
                    return;
                }
                //DOContourGeneral contourGeneral = doGrid3d.ContourOption.ContourGeneral;//从主程序取得显示选项控制信息
                HA.Data.DisplayOptions2.DOContour  contourOptionV2 = doGrid3d.Contour;

                if (contourOptionV2 == null)
                {
                    return;
                }



                contourSetup.UpdateVisibility ( doGrid3d.IsShowContour);

                contourSetup.UpdateContourModel ( CONTOURMODE.LayerContours);


             
                if (contourOptionV2.ContourLines == null)
                {
                    return;
                }

                Dictionary<double, System.Drawing.Color> contourLines = new Dictionary<double, System.Drawing.Color>();


                for (int i = 0; i < contourOptionV2.ContourLines.Count; i++)
                {
                    System.Drawing.Color color = System.Drawing.Color.FromArgb(contourOptionV2.ContourLines[i].Color.A, contourOptionV2.ContourLines[i].Color.R, contourOptionV2.ContourLines[i].Color.G, contourOptionV2.ContourLines[i].Color.B);

                    contourLines.Add(contourOptionV2.ContourLines[i].Value, color);

                }

        
                contourSetup.UpdateContourLineValue(contourLines);



                HA.Data.DisplayOptions2.ContourLineScalarBar contourLinescalarBar = doGrid3d.Contour.ContourLineProperty.LineScalarBar;



                ContourScalarBar scalarBar = new ContourScalarBar();


                scalarBar.IsShowScalarBar = contourLinescalarBar.IsShowScalarBar;
                scalarBar.IsDragable = contourLinescalarBar.IsDragable;
                scalarBar.Title = contourLinescalarBar.Title;
                scalarBar.TitleColor = contourLinescalarBar.TitleColor;
                scalarBar.TitleFontSize = contourLinescalarBar.TitleFontSize;
                scalarBar.LabelColor = contourLinescalarBar.LabelColor;
                scalarBar.LabelFontSize = contourLinescalarBar.LabelFontSize;




                //主程序认为视口的左上角点为 相对坐标为 0  0
                //vtk 图例的位置设置的为图例左下角交点坐标  需要转换

                int sizeW = vtkScene.VtkRenderWindow.GetSize()[0];//视口宽度   像素
                int sizeH = vtkScene.VtkRenderWindow.GetSize()[1];//视口高度  像素

                //scalarBar.YLowLeftToViewPort = sizeH - contourLinescalarBar.Height *sizeH - contourLinescalarBar.YToViewPortLeftUper ;
                //scalarBar.Width = contourLinescalarBar.Width;
                //scalarBar.Height = contourLinescalarBar.Height;
                //scalarBar.XLowLeftToViewPort = contourLinescalarBar.XToViewPortLeftUper ;
               

                //scalarBar.Width = 0.04;
                //scalarBar.Height = 0.2;
                //scalarBar.XLowLeftToViewPort = sizeW * 0.5;
                //scalarBar.YLowLeftToViewPort = sizeH * 0.5;   
                //width  height  coor  转换到0-1范围
                scalarBar.Width = (double)contourLinescalarBar.Width * 0.01;
                scalarBar.Height = (double)contourLinescalarBar.Height * 0.01;
                scalarBar.XLowLeftToViewPort = (double)contourLinescalarBar.XToViewPortLeftUper * 0.01;
                scalarBar.YLowLeftToViewPort =(double )( sizeH - contourLinescalarBar.Height * 0.01 * sizeH - contourLinescalarBar.YToViewPortLeftUper * 0.01 * sizeH)/(double )sizeH ;



                contourSetup.UpdateScalarBar(scalarBar);



                HA.Data.DisplayOptions2.ContourLineTagging contourLineTagging = doGrid3d.Contour.ContourLineProperty.LineTagging;

                ContourTagging tagging = new ContourTagging();


                tagging.IsShowTagging = contourLineTagging.IsShowTagging;
                tagging.TagColor = contourLineTagging.TagColor;
                tagging.FontSize = contourLineTagging.FontSize;
                tagging.MaskPointNum = contourLineTagging.MaskPointNum;


                contourSetup.UpdateTagging(tagging);



                ContourLineProperty contourLineProperty = new ContourLineProperty();

                switch (contourOptionV2.ContourType)
                {
                    case ContourDisplayType.Linear:
                        contourLineProperty.ContourFillOrLine = CONTOURTYPE.Linear;
                        break;
                    case ContourDisplayType.ColorFill:
                        contourLineProperty.ContourFillOrLine = CONTOURTYPE.ColorFill;
                        break;
                    case ContourDisplayType.LinearAndColorFill:
                        contourLineProperty.ContourFillOrLine = CONTOURTYPE.LinearAndColorFill;
                        break;
                    default:
                        break;
                }

                contourLineProperty.IsSpecifyPrecision = contourOptionV2.IsSpecifyPrecision;

                contourLineProperty.DecimalDigitNum = contourOptionV2.DecimalDigitNum;

                contourLineProperty.IsUseScientificNotation = contourOptionV2.IsUseScientificNotation;


                contourLineProperty.IsUseCellRealData = contourOptionV2.IsUseCellRealData;


                contourLineProperty.LineWidth = contourOptionV2.LineWidth;

                contourLineProperty.LineColor = contourOptionV2.LineColor;


                contourSetup.UpdateContourLineProperty(contourLineProperty);



                #endregion



            }
            catch
            {

               
                return;
            }




        }
      
        /// <summary>
        /// 从主程序取得VectorSetup设置到vecSetup
        /// </summary>
        /// <param name="project"></param>
        /// <param name="vtkScene"></param>
        /// <param name="vecSetup"></param>
        private static void UpdateVectorSetup(Project project, Scene vtkScene, VectorSetup vecSetup)
        {
            if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }
            if (vecSetup == null)
            {
                return;
            }

            try
            {
                //DOGrid3d doGrid3d = project.DisplayOption.Grid3d;

                //if (doGrid3d == null)
                //{
                //    return;
                //}
                ////DOContourGeneral contourGeneral = doGrid3d.ContourOption.ContourGeneral;//从主程序取得显示选项控制信息
                ////DOContourOptionV2 contourOptionV2 = doGrid3d.Contour;

                //DOVector vectorSetup = doGrid3d.Vector;


                //if (vectorSetup == null)
                //{
                //    return;
                //}


                //vecSetup.IsShowVector = doGrid3d.IsShowVector;

                //vecSetup.IsVaryLength = vectorSetup.IsVaryLength;

                //vecSetup.TotalLength = vectorSetup.Length;

                //vecSetup.ScaleRatio = vectorSetup.Scaling;

                ////vecSetup.Color = System.Drawing.Color.FromArgb(vectorSetup.VectorColor.A, vectorSetup.VectorColor.R, vectorSetup.VectorColor.G, vectorSetup.VectorColor.B);


            }
            catch
            {


            }
        }



        /// <summary>
        /// 从主程序取得VectorSetup设置到vecSetup
        /// </summary>
        /// <param name="project"></param>
        /// <param name="vtkScene"></param>
        /// <param name="vecSetup"></param>
        private static void UpdateVectorSetup2(Project project, Scene vtkScene, VectorSetup vecSetup)
        {
            if (project == null)
            {
                return;
            }
            if (vtkScene == null)
            {
                return;
            }
            if (vecSetup == null)
            {
                return;
            }

            try
            {
               HA.Data .DisplayOptions2 . DOGrid3d doGrid3d = project.DisplayOption2 .DoGrid3dData .Grid3d ;

                if (doGrid3d == null)
                {
                    return;
                }
                //DOContourGeneral contourGeneral = doGrid3d.ContourOption.ContourGeneral;//从主程序取得显示选项控制信息
                //DOContourOptionV2 contourOptionV2 = doGrid3d.Contour;

                HA.Data.DisplayOptions2.DOVector vectorSetup = doGrid3d.Vector;


                if (vectorSetup == null)
                {
                    return;
                }


                vecSetup.IsShowVector = doGrid3d.IsShowVector;

                vecSetup.IsVaryLength = vectorSetup.IsVaryLength;

                vecSetup.TotalLength = vectorSetup.Length;

                vecSetup.ScaleRatio = vectorSetup.Scaling;

                vecSetup.Color = System.Drawing.Color.FromArgb(vectorSetup.VectorColor .A ,vectorSetup .VectorColor .R,vectorSetup .VectorColor .G,vectorSetup.VectorColor.B );



            }
            catch
            {


            }
        }

        #endregion
    }
}
